package com.bigdata.schaduler.service;

import com.bigdata.schaduler.client.DolphinClient;
import com.bigdata.schaduler.util.PageInfo;
import com.bigdata.schaduler.domain.ProcessInstanceQueryParam;
import org.apache.dolphinscheduler.common.enums.HttpMethod;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
import org.apache.dolphinscheduler.dao.entity.TaskInstance;

import java.util.List;

public class ProcessInstanceService {

  private final DolphinClient dolphinClient;

  public ProcessInstanceService(DolphinClient dolphinClient) {
    this.dolphinClient = dolphinClient;
  }

  public String getProcessInstanceUri(Long projectCode) {
    return "projects/" + projectCode + "/process-instances";
  }

  public PageInfo<ProcessInstance> selectPageList(Long projectCode, ProcessInstanceQueryParam param) {
    String uri = getProcessInstanceUri(projectCode);
    return dolphinClient.requestFormData(HttpMethod.GET, uri, param, PageInfo.class);
  }

  public List<TaskInstance> selectTaskInstanceList(Long projectCode, Long instanceId) {
    String uri = getProcessInstanceUri(projectCode) + "/" + instanceId + "/com/bigdata/schaduler/tasks";
    return dolphinClient.requestFormData(HttpMethod.GET, uri, null, List.class);
  }

  public ProcessInstance select(Long projectCode, Long instanceId) {
    String uri = getProcessInstanceUri(projectCode) + "/" + instanceId;
    return dolphinClient.requestFormData(HttpMethod.GET, uri, null, ProcessInstance.class);
  }
}
